package guoanmaker.provider.business.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import guoanmaker.provider.business.modelnobind.Appraise;
import guoanmaker.provider.business.modelnobind.AppraiseNumbers;
import guoanmaker.provider.business.modelnobind.ProvideOrderFormForOrder;
import guoanmaker.provider.helper.common.util.SystemHelper;


/**
 * 
* @ClassName: PersonalAppraiseDao 
* @Description: 查询评价dao
* @author Yvonne_MGL
* @date 2017年8月29日 下午6:57:00 
*  
 */
@Repository
public class PersonalAppraiseDao {
	@PersistenceContext
	EntityManager em;
	
	/**
	 * 
	* @Description: 查询服务商评价详情 
	* @param businessid 服务商id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月29日 下午9:54:14     
	* @return Map    
	* @throws 
	 */
	public Map getBusinessAppraise(String businessid, Integer pageNumber, Integer pageSize){
		Map map=new HashMap();
		String jpql="select new guoanmaker.provider.business.modelnobind.Appraise(c.commodityName,a.averageValue,"
				+ "u.nickname,a.appraiseTime,a.appraise,a.info,u.portrait,u.id,a.reply,a.replyTime,u.username,a.id) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.isShow='1' order by a.appraiseTime desc";
		Query query = em.createQuery(jpql);
		List<Appraise> list=query.getResultList();
		map.put("totle",list.size());
		if(pageNumber==null){
			pageNumber=0;
		}
		if (pageSize==null) {
			pageSize=10;
		}
		query.setFirstResult(pageNumber*pageSize); // 序号从0 开始
		query.setMaxResults(pageSize);

		List<Appraise> result = query.getResultList();
		map.put("appraises", result);
		
		Double completeAverage=5.0;//完成质量平均分
		Double communicationAverage=5.0;//沟通良好平均分
		Double serveAverage=5.0;//服务效率平均分
		Double completeSum=0.0;
		Double communicationSum=0.0;
		Double serveSum=0.0;
		int completeTotle=0;
		int communicationTotle=0;
		int serveTotle=0;
		if(list!=null&&list.size()>0){
			for (int i = 0; i < list.size(); i++) {
				String[] infos=list.get(i).getInfo().split(",");
				if(infos.length>0){
					for (int j = 0; j < infos.length; j++) {
						String str=infos[j];
						String[] strs=str.split("@");
						if(strs[0].equals("完成质量")){
							completeSum+=Double.parseDouble(strs[1]);
							completeTotle+=1;
						}else if(strs[0].equals("沟通良好")){
							communicationSum+=Double.parseDouble(strs[1]);
							communicationTotle+=1;
						}else if(strs[0].equals("服务效率")){
							serveSum+=Double.parseDouble(strs[1]);
							serveTotle+=1;
						}
					}
				}
			}
		}
		if(completeTotle!=0){
			completeAverage=new BigDecimal(completeSum/completeTotle).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		if(communicationTotle!=0){
			communicationAverage=new BigDecimal(communicationSum/communicationTotle).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		if(serveTotle!=0){
			serveAverage=new BigDecimal(serveSum/serveTotle).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		map.put("completeAverage", completeAverage);
		map.put("communicationAverage", communicationAverage);
		map.put("serveAverage", serveAverage);
		
		String jpqlrecommend="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue>=3.0 and a.isShow='1'";
		Query queryrecommend = em.createQuery(jpqlrecommend);
		Long recommend=(Long)queryrecommend.getResultList().get(0);
		map.put("recommend", recommend);
		
		String jpqlNorecommend="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue<3.0 and a.isShow='1'";
		Query queryNorecommend = em.createQuery(jpqlNorecommend);
		Long norecommend=(Long)queryNorecommend.getResultList().get(0);
		map.put("norecommend", norecommend);
		
		Double recommendValue=100.0;
		if(recommend+recommend>0){
			recommendValue=new BigDecimal((double)recommend*100/((double)recommend+(double)norecommend)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		map.put("recommendValue", recommendValue+"%");
		
		String jpqlWeekPraise="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue>4.0 and a.isShow='1' and a.appraiseTime>='"+SystemHelper.getLess7Day()+"'" ;
		Query queryWeekPraise = em.createQuery(jpqlWeekPraise);
		Long weekPraise=(Long)queryWeekPraise.getResultList().get(0);
		
		String jpqlMonthPraise="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue>4.0 and a.isShow='1' and a.appraiseTime>='"+SystemHelper.getLessMonth(1)+"'" ;
		Query queryMonthPraise = em.createQuery(jpqlMonthPraise);
		Long monthPraise=(Long)queryMonthPraise.getResultList().get(0);
		
		String jpqlSixMonthPraise="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue>4.0 and a.isShow='1' and a.appraiseTime>='"+SystemHelper.getLessMonth(6)+"'" ;
		Query querySixMonthPraise = em.createQuery(jpqlSixMonthPraise);
		Long sixMonthPraise=(Long)querySixMonthPraise.getResultList().get(0);
		
		String jpqlSixMonthAgoPraise="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue>4.0 and a.isShow='1' and a.appraiseTime<'"+SystemHelper.getLessMonth(6)+"'" ;
		Query querySixMonthAgoPraise = em.createQuery(jpqlSixMonthAgoPraise);
		Long sixMonthAgoPraise=(Long)querySixMonthAgoPraise.getResultList().get(0);
		
		AppraiseNumbers praise=new AppraiseNumbers();
		praise.setAppraiseType("1");
		praise.setWeekNum(weekPraise);
		praise.setMonthNum(monthPraise);
		praise.setSixMonthNum(sixMonthPraise);
		praise.setSixMonthAgoNum(sixMonthAgoPraise);
		praise.setAllNum(sixMonthPraise+sixMonthAgoPraise);
		
		String jpqlWeekMiddleReview="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue<=4.0 and a.isShow='1' and a.averageValue>2.0 and a.appraiseTime>='"+SystemHelper.getLess7Day()+"'" ;
		Query queryWeekMiddleReview = em.createQuery(jpqlWeekMiddleReview);
		Long weekMiddleReview=(Long)queryWeekMiddleReview.getResultList().get(0);
		
		String jpqlMonthMiddleReview="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue<=4.0 and a.isShow='1' and a.averageValue>2.0 and a.appraiseTime>='"+SystemHelper.getLessMonth(1)+"'" ;
		Query queryMonthMiddleReview = em.createQuery(jpqlMonthMiddleReview);
		Long monthMiddleReview=(Long)queryMonthMiddleReview.getResultList().get(0);
		
		String jpqlSixMonthMiddleReview="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue<=4.0 and a.isShow='1' and a.averageValue>2.0 and a.appraiseTime>='"+SystemHelper.getLessMonth(6)+"'" ;
		Query querySixMonthMiddleReview = em.createQuery(jpqlSixMonthMiddleReview);
		Long sixMonthMiddleReview=(Long)querySixMonthMiddleReview.getResultList().get(0);
		
		String jpqlSixMonthAgoMiddleReview="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue<=4.0 and a.isShow='1' and a.averageValue>2.0 and a.appraiseTime<'"+SystemHelper.getLessMonth(6)+"'" ;
		Query querySixMonthAgoMiddleReview = em.createQuery(jpqlSixMonthAgoMiddleReview);
		Long sixMonthAgoMiddleReview=(Long)querySixMonthAgoMiddleReview.getResultList().get(0);
		
		AppraiseNumbers middleReview=new AppraiseNumbers();
		middleReview.setAppraiseType("2");
		middleReview.setWeekNum(weekMiddleReview);
		middleReview.setMonthNum(monthMiddleReview);
		middleReview.setSixMonthNum(sixMonthMiddleReview);
		middleReview.setSixMonthAgoNum(sixMonthAgoMiddleReview);
		middleReview.setAllNum(sixMonthMiddleReview+sixMonthAgoMiddleReview);
		
		String jpqlWeekBadReview="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue<=2.0 and a.isShow='1' and a.appraiseTime>='"+SystemHelper.getLess7Day()+"'" ;
		Query queryWeekBadReview = em.createQuery(jpqlWeekBadReview);
		Long weekBadReview=(Long)queryWeekBadReview.getResultList().get(0);
		
		String jpqlMonthBadReview="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue<=2.0 and a.isShow='1' and a.appraiseTime>='"+SystemHelper.getLessMonth(1)+"'" ;
		Query queryMonthBadReview = em.createQuery(jpqlMonthBadReview);
		Long monthBadReview=(Long)queryMonthBadReview.getResultList().get(0);
		
		String jpqlSixMonthBadReview="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue<=2.0 and a.isShow='1' and a.appraiseTime>='"+SystemHelper.getLessMonth(1)+"'" ;
		Query querySixMonthBadReview = em.createQuery(jpqlSixMonthBadReview);
		Long sixMonthBadReview=(Long)querySixMonthBadReview.getResultList().get(0);
		
		String jpqlSixMonthAgoBadReview="select count(a) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.averageValue<=2.0 and a.isShow='1' and a.appraiseTime<'"+SystemHelper.getLessMonth(6)+"'" ;
		Query querySixMonthAgoBadReview = em.createQuery(jpqlSixMonthAgoBadReview);
		Long sixMonthAgoBadReview=(Long)querySixMonthAgoBadReview.getResultList().get(0);
		
		AppraiseNumbers badReview=new AppraiseNumbers();
		badReview.setAppraiseType("3");
		badReview.setWeekNum(weekBadReview);
		badReview.setMonthNum(monthBadReview);
		badReview.setSixMonthNum(sixMonthBadReview);
		badReview.setSixMonthAgoNum(sixMonthAgoBadReview);
		badReview.setAllNum(sixMonthBadReview+sixMonthAgoBadReview);
		
		AppraiseNumbers totle=new AppraiseNumbers();
		totle.setAppraiseType("4");
		totle.setWeekNum(weekPraise+weekMiddleReview+weekBadReview);
		totle.setMonthNum(monthPraise+monthMiddleReview+monthBadReview);
		totle.setSixMonthNum(sixMonthPraise+sixMonthMiddleReview+sixMonthBadReview);
		totle.setSixMonthAgoNum(sixMonthAgoPraise+sixMonthAgoMiddleReview+sixMonthAgoBadReview);
		totle.setAllNum(sixMonthPraise+sixMonthAgoPraise+sixMonthMiddleReview+sixMonthAgoMiddleReview+sixMonthBadReview+sixMonthAgoBadReview);
		
		List<AppraiseNumbers> appraiseNumbers=new ArrayList<>();
		appraiseNumbers.add(praise);
		appraiseNumbers.add(middleReview);
		appraiseNumbers.add(badReview);
		appraiseNumbers.add(totle);
		
		map.put("appraiseNumbers", appraiseNumbers);
		
		return map;
	}

	public Map getAppraise(String businessid, Integer pageNumber, Integer pageSize) {
		Map map=new HashMap();
		String jpql="select new guoanmaker.provider.business.modelnobind.Appraise(c.commodityName,a.averageValue,"
				+ "u.nickname,a.appraiseTime,a.appraise,a.info,u.portrait,u.id,a.reply,a.replyTime,u.username,a.id) from PersonalAppraise a left join a.orderForm o left join o.user u "
				+ "left join o.commodity c left join c.providerBusiness b where b.id='"+businessid+"' and a.isShow='1' order by a.appraiseTime desc";
		Query query = em.createQuery(jpql);
		List<Appraise> list=query.getResultList();
		map.put("totle",list.size());
		if(pageNumber==null){
			pageNumber=0;
		}
		if (pageSize==null) {
			pageSize=10;
		}
		query.setFirstResult(pageNumber*pageSize); // 序号从0 开始
		query.setMaxResults(pageSize);

		List<Appraise> result = query.getResultList();
		map.put("appraises", result);
		return map;
	}
}
